package com.spring.sunflower.common;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseCompatActivity;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.util.ChannelInfoUtils;
import com.spring.sunflower.widget.ToggleView;

/**
 * Created by Kevin on 2021/7/13<br/>
 * <p>
 * Describe:<br/>
 */
public class AboutActivity extends BaseCompatActivity implements View.OnClickListener {
    private RelativeLayout rlAgreement, rlPrivateAgreement, rlReport, rlToggle;
    private ImageView ivLogo;
    private int clickTime = 0;
    private int titleClickTime = 0;
    private ToggleView toggleView;
    private String channel;

    @Override
    public void initView() {
        tvTitle.setText("关于我们");
        rlAgreement = findViewById(R.id.rlAgreement);
        rlPrivateAgreement = findViewById(R.id.rlPrivateAgreement);
        rlReport = findViewById(R.id.rlReport);
        rlToggle = findViewById(R.id.rlToggle);
        ivLogo = findViewById(R.id.ivLogo);
        toggleView = findViewById(R.id.toggle);
        channel = ChannelInfoUtils.getChannel(this);

        rlAgreement.setOnClickListener(this);
        rlPrivateAgreement.setOnClickListener(this::onClick);
        rlReport.setOnClickListener(this::onClick);
        toggleView.setDelayChecked(false);
        boolean showSwitchEnvironmentDialog = getField(Constant.switchEnvironmentDialogShow, false);
        if (showSwitchEnvironmentDialog) {
            rlToggle.setVisibility(View.VISIBLE);
            toggleView.setChecked(true);
        }
        tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ++titleClickTime;
                if (titleClickTime >= 16) {
                    showToast("your channel is " + channel);
                    titleClickTime = 10;
                }
            }
        });
        ivLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean showSwitch = getField(Constant.switchEnvironmentDialogShow, false);
                ++clickTime;
                if (clickTime >= 16 && clickTime < 20) {
                    if (showSwitch) {
                        showToast("你已开启切换环境模式");
                        return;
                    }
                    showToast("剩余" + (20 - clickTime) + "次即可开启切换环境模式");
                } else if (clickTime >= 20) {
                    if (showSwitch) {
                        showToast("你已开启切换环境模式");
                        return;
                    }
                    showToast("你已开启切换环境模式，下次登录可切换环境");
                    saveField(Constant.switchEnvironmentDialogShow, true);
                    rlToggle.setVisibility(View.VISIBLE);
                    toggleView.setChecked(true);
                }
            }
        });
        toggleView.setOnToggleClickListener(new ToggleView.OnToggleClickListener() {
            @Override
            public void onToggleClick(boolean toggleState) {
                if (toggleState) {
                    saveField(Constant.switchEnvironmentDialogShow, true);
                } else {
                    saveField(Constant.switchEnvironmentDialogShow, false);
                    rlToggle.setVisibility(View.GONE);
                    clickTime = 0;
                }
                toggleView.setChecked(toggleState);
            }
        });
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_about;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlAgreement:
                goAgreement();
                break;
            case R.id.rlPrivateAgreement:
                goPrivateAgreement();
                break;
            case R.id.rlReport:
                startNewActivity(FeedbackActivity.class);
                break;
        }
    }
}
